package br.com.emanuelvictor.architectureandroid.app1.util;

import java.io.File;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import android.os.Environment;
import android.util.Log;

public class HistoryPath implements Serializable {

	// Deve ser serializable para o savedState
	private static final long serialVersionUID = 1L;

	private String root;
	// Path Atual
	private String path;
	// Path Anteior, para o backStack
	private String oldPath;
	// Variável que identifica se entrou pelo histórico ou pela today
	private List<String> history = new LinkedList<String>();

	public static HistoryPath newInstance() {
		if (HistoryPath.isExternalStorageReadable()) {
			if (HistoryPath.isExternalStorageWritable()) {
				String path = Environment.getExternalStorageDirectory()
						.getAbsolutePath();
				HistoryPath history = new HistoryPath(path);
				history.getHistory().add(path);
				return history;
			}
		}
		return null;
	}

	public List<String> getHistory() {
		return history;
	}

	public void setHistory(List<String> history) {
		this.history = history;
	}

	public void restart() {
		this.history = null;
		this.history = new LinkedList<String>();
		this.history.add(Environment.getExternalStorageDirectory()
				.getAbsolutePath());
		this.refreshHistory();
	}

	private HistoryPath(String path) {
		super();
		this.setRoot(Environment.getExternalStorageDirectory()
				.getAbsolutePath());
		this.history.add(path);
		Log.d("Create History", "Create History");
		this.refreshHistory();
	}

	private void refreshHistory() {

		this.path = this.history.get(this.history.size() - 1);
		try {
			this.oldPath = this.history.get(this.history.size() - 2);
		} catch (Exception e) {
			this.oldPath = null;
		}

		if (this.oldPath == this.path) {
			this.oldPath = null;
		}

		if (this.oldPath != null) {
			Log.d("old path", oldPath);
		} else {
			Log.d("old path", "null");
		}

		Log.d("path", path);

	}

	public boolean isRoot() {
		return this.oldPath == null;
	}

	public String getOldPath() {
		return this.oldPath;
	}

	public void decrementHistory() {
		this.history.remove(this.history.size() - 1);
		this.refreshHistory();
	}

	public void incrementHistory(String path) {
		this.history.add(path);
		this.refreshHistory();
	}

	public File getFile() {
		File file = new File(path);
		return file;
	}

	public String getPath() {
		return this.history.get(this.history.size() - 1);
	}

	public static boolean isExternalStorageWritable() {
		String state = Environment.getExternalStorageState();
		return Environment.MEDIA_MOUNTED.equals(state);
	}

	public static boolean isExternalStorageReadable() {
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)
				|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			return true;
		}
		return false;
	}

	public String getRoot() {
		return root;
	}

	public void setRoot(String root) {
		this.root = root;
	}

}
